package com.rjwh.dingdong.client.view;

import android.content.Context;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.rjwh.dingdong.client.R;

import java.util.ArrayList;
import java.util.List;

/**
 * Copyright (c) 2014 All rights reserved 名称：BottomBar.java
 * 描述：主页面的底部标题栏，用于切换fragment
 *
 * @author lv
 * @version v1.0
 * @date：2014年6月23日 下午2:48:37
 */
public class BottomBar extends RelativeLayout implements OnClickListener {

    private LayoutInflater inflater;
    private LinearLayout barLayout;
    private LinearLayout actionLL;
    private static int hostView;
    private static FragmentManager fragmentManager;
    private static FragmentTransaction fragmentTransaction;

    public BottomBar(Context context, AttributeSet attrs) {
        super(context, attrs);
        inflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        initView();
    }

    /**
     * 描述：自定义底部视图的加载
     */
    private void initView() {
        barLayout = (LinearLayout) inflater.inflate(R.layout.bottombar, null);// 底图
        addView(barLayout);// 添加到当前视图中
        actionLL = (LinearLayout) barLayout.findViewById(R.id.tab_group);
        lpLl = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT,
                LayoutParams.MATCH_PARENT, 1.0f);
        tabList = new ArrayList<Tab>();
    }

    /**
     * 描述：获取到Fragment的管理器
     *
     * @param mhostView ：切换锚点
     * @param act       ：父容器
     */
    public void init(int mhostView, FragmentActivity act) {
        hostView = mhostView;
        fragmentManager = act.getSupportFragmentManager();
    }

    /**
     * 描述：添加tab到父视图
     *
     * @param tab ： 需要被添加的tab
     */
    public void addTab(Tab tab) {
        int index = actionLL.getChildCount();
        addTab(tab, index);
        tabList.add(tab);
    }

    private List<Integer> hashcodes = new ArrayList<Integer>();
    private View preTab = null;
    private LinearLayout.LayoutParams lpLl;
    private List<Tab> tabList;

    public void addTab(Tab tab, int index) {
        View tabView = inflateTab(tab);
        hashcodes.add(tabView.hashCode());
        TextView tv = (TextView) tabView.findViewById(R.id.actionbar_TV);
        ImageView iv = (ImageView) tabView.findViewById(R.id.actionbar_IBT);
        tab.setImageView(iv);
        tab.setTextView(tv);
        if (tab.isFoucesd()) {
            preTab = tabView;
            tab.performAction(null);
            setState(tab, iv, tv, true);
            tv.setTextColor(getResources()
                    .getColor(R.color.blue_button_nor));
        } else {
            setState(tab, iv, tv, false);
        }
        actionLL.addView(tabView, index, lpLl);
    }

    public void changeTabText(int index, String text) {
        View tabView = actionLL.getChildAt(index);
        if (tabView != null) {
            TextView barTV = (TextView) tabView.findViewById(R.id.actionbar_TV);
            barTV.setText(text);
        }
    }

    /**
     * 描述：设置tab的选中状态，切换底图
     *
     * @param tab       ：被操作的tab
     * @param iv        ：tab的图片
     * @param isPressed ：当前tab是否被选中了
     */
    private void setState(Tab tab, ImageView iv, TextView tv, boolean isPressed) {
        if (isPressed) {
            tv.setTextColor(getResources()
                    .getColor(R.color.blue_button_nor));
        } else {
            tv.setTextColor(getResources().getColor(R.color.bottombar_normal));
        }
        TabSelectImage stImage = tab.getTabSelectImage();
        if (stImage != null) {
            if (isPressed) {
                iv.setImageResource(stImage.getMoveImage());
            } else {
                iv.setImageResource(stImage.getDefaultImage());
            }
        }

    }

    /**
     * 描述：加载出tab的视图
     *
     * @param 需要加载的tab对象
     * @return
     */
    private View inflateTab(Tab tab) {
        View view = inflater.inflate(R.layout.bottombar_btn, actionLL, false);
        ImageView tabView = (ImageView) view.findViewById(R.id.actionbar_IBT);
        TextView barTV = (TextView) view.findViewById(R.id.actionbar_TV);
        ImageView barTips = (ImageView) view.findViewById(R.id.actionbar_tips);
        setState(tab, tabView, barTV, false);
        if (tab.getText() != null) {
            barTV.setText(tab.getText());
            barTV.setVisibility(View.VISIBLE);
        } else {
            barTV.setVisibility(View.GONE);
        }
        view.setTag(tab);
        view.setOnClickListener(this);
        return view;
    }

    public void hideTipsOnTab(int index) {
        View tabView = actionLL.getChildAt(index);
        ImageView barTips = (ImageView) tabView
                .findViewById(R.id.actionbar_tips);
        barTips.setVisibility(View.GONE);

    }

    public void showTipsOnTab(int index) {
        View tabView = actionLL.getChildAt(index);
        ImageView barTips = (ImageView) tabView
                .findViewById(R.id.actionbar_tips);
        barTips.setVisibility(View.VISIBLE);
    }

    public interface Tab {
        public int getIndex();

        public String getText();

        public ImageView getImageView();

        public TextView getTextView();

        public void setImageView(ImageView view);

        public void setTextView(TextView view);

        public void performAction(View view);

        public boolean isFoucesd();

        public TabSelectImage getTabSelectImage();
    }

    public static abstract class AbstractTab implements Tab {
        private int mIndex = 0;
        private String mTitle;
        private boolean mFoucse;
        private ImageView mIV;
        private TextView mTV;
        private TabSelectImage mStImage;

        public AbstractTab(int index, String title, TabSelectImage stImage,
                           boolean fousce) {
            mIndex = index;
            mTitle = title;
            mStImage = stImage;
            mFoucse = fousce;
        }

        @Override
        public int getIndex() {
            return mIndex;
        }

        @Override
        public String getText() {
            return mTitle;
        }

        @Override
        public boolean isFoucesd() {
            return mFoucse;
        }

        @Override
        public ImageView getImageView() {
            return mIV;
        }

        @Override
        public void setImageView(ImageView view) {
            mIV = view;
        }

        @Override
        public TextView getTextView() {
            return mTV;
        }

        @Override
        public void setTextView(TextView view) {
            mTV = view;
        }

        @Override
        public TabSelectImage getTabSelectImage() {
            // TODO Auto-generated method stub

            return mStImage;
        }
    }

    public static class TabSelectImage {
        private int defaultImage, moveImage;

        public int getDefaultImage() {
            return defaultImage;
        }

        public void setDefaultImage(int defaultImage) {
            this.defaultImage = defaultImage;
        }

        public int getMoveImage() {
            return moveImage;
        }

        public void setMoveImage(int moveImage) {
            this.moveImage = moveImage;
        }

        public TabSelectImage(int defaultImage, int moveImage) {
            super();
            this.defaultImage = defaultImage;
            this.moveImage = moveImage;
        }

    }

    public static class FragmentTab extends AbstractTab {
        private Fragment mFragment;

        /**
         * 描述：Tab的具体实现类
         *
         * @param fragment ： 所对应的fragment
         * @param index    : 当前tab的唯一标识
         * @param title    ：tab上的文字
         * @param fouced   ：改tab是否设置为默认选中tab
         */
        public FragmentTab(Fragment fragment, int index, String title,
                           TabSelectImage stImage, boolean fouced) {
            super(index, title, stImage, fouced);
            mFragment = fragment;
        }

        @Override
        public void performAction(View view) {
            fragmentTransaction = fragmentManager.beginTransaction();
            List<Fragment> listFragment = fragmentManager.getFragments();
            if (listFragment != null) {
                for (Fragment fm : listFragment) {
                    // 这里是判断已存在推展中的Fm 是已经添加了的 就将它设置为隐藏
                    if (fm != null && fm.isAdded())
                        fragmentTransaction.hide(fm);
                }
            }
            if (mFragment.isAdded()) {
                fragmentTransaction.show(mFragment);
            } else {
                fragmentTransaction.add(hostView, mFragment);
            }
            fragmentTransaction.commitAllowingStateLoss();
        }

    }

    @Override
    public void onClick(View v) {

        if (((Tab) preTab.getTag()).getIndex() == ((Tab) v.getTag()).getIndex()) {
            return;
        }

        Object preObj = preTab.getTag();
        if (preObj instanceof Tab) {
            Tab preT = (Tab) preObj;
            TextView preTV = (TextView) preTab.findViewById(R.id.actionbar_TV);
            ImageView preIV = (ImageView) preTab
                    .findViewById(R.id.actionbar_IBT);
            setState(preT, preIV, preTV, false);
        }

        TextView tv = (TextView) v.findViewById(R.id.actionbar_TV);
        ImageView iv = (ImageView) v.findViewById(R.id.actionbar_IBT);

        preTab = v;
        Object obj = v.getTag();
        if (obj instanceof Tab) {
            Tab tab = (Tab) obj;
            setState(tab, iv, tv, true);
            tab.performAction(v);
        }
    }

    public void setTabVisible(int index) {
        for (Tab tab : tabList) {
            if (tab.getIndex() == index) {
                setState(tab, tab.getImageView(), tab.getTextView(), true);
            } else {
                setState(tab, tab.getImageView(), tab.getTextView(), false);
            }
        }
    }

    /**
     * 修改选中按钮
     *
     * @param @param index
     * @return void
     * @throws
     * @Title: changeTabSateShow
     * @Description: TODO(这里用一句话描述这个方法的作用)
     * @history 1.YYYY-MM-DD author: description:
     */
    public void changeTabSateShow(int index) {
        try {
            TextView preTV = (TextView) preTab.findViewById(R.id.actionbar_TV);
            ImageView preIV = (ImageView) preTab
                    .findViewById(R.id.actionbar_IBT);
            Object preObj = preTab.getTag();
            if (preObj instanceof Tab) {
                Tab preT = (Tab) preObj;
                setState(preT, preIV, preTV, false);
            }

            View v = actionLL.getChildAt(index);

            TextView tv = (TextView) v.findViewById(R.id.actionbar_TV);
            ImageView iv = (ImageView) v.findViewById(R.id.actionbar_IBT);

            preTab = v;
            Object obj = v.getTag();
            if (obj instanceof Tab) {
                Tab tab = (Tab) obj;
                setState(tab, iv, tv, true);
                tab.performAction(v);
            }
        } catch (Exception e) {
        }
    }
}
